package com.msc.mc.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;

public class NIODesignDataImport {
	
	public static void main( String[] args ) throws Exception {
		NIODesignDataImport ddImport = new NIODesignDataImport( "2017-c1" , "aBS" , "1111" , "123" , "456");
		ddImport.createImportZipFile("/data/" );
	}
	
	private long timeSeq = 0 ;
	
	public NIODesignDataImport(String matSpecNumber, String materialSymbol, String matId, String nioMatId,
			String nioSepcId) {
		super();
		this.matSpecNumber = matSpecNumber;
		this.materialSymbol = materialSymbol;
		this.matId = matId;
		this.nioMatId = nioMatId;
		this.nioSepcId = nioSepcId;
		this.name =  this.materialSymbol  + ";" + this.matSpecNumber;
		
		
		Date now = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		timeSeq = Long.parseLong( sdf.format( now ) );
		
	}


	private String matSpecNumber ;
	private String materialSymbol;
	private String name ;
	private String matId;
	
	private String nioMatId ;
	private String nioSepcId ;
	
	
	public String getMatSpecNumber() {
		return matSpecNumber;
	}

	public void setMatSpecNumber(String matSpecNumber) {
		this.matSpecNumber = matSpecNumber;
	}

	public String getMaterialSymbol() {
		return materialSymbol;
	}

	public void setMaterialSymbol(String materialSymbol) {
		this.materialSymbol = materialSymbol;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getMatId() {
		return matId;
	}

	public void setMatId(String matId) {
		this.matId = matId;
	}

	public String getNioMatId() {
		return nioMatId;
	}

	public void setNioMatId(String nioMatId) {
		this.nioMatId = nioMatId;
	}

	public String getNioSepcId() {
		return nioSepcId;
	}

	public void setNioSepcId(String nioSepcId) {
		this.nioSepcId = nioSepcId;
	}


	public String createImportZipFile( String scriptWorkingDir   ) throws Exception {
		SimManagerImportBean importBean = new SimManagerImportBean();
		importBean.addParam("canEditDuringImport", "false");
		importBean.addParam("useOidDuringImport", "false");
		
		ObjectListBean nioMaterial  = importBean.addObjectList( "NIOMaterial" , "[objectId=='" + nioMatId + "']", true );
		ObjectListBean nioMatSpec   = importBean.addObjectList( "NIOMatSpecification" , "[objectId=='" + nioSepcId + "']", true);
		ObjectListBean matSchemal   = importBean.addObjectList( "MatMaterialsSchema" , "[name == 'NIODesignData']", true );
		
		 
		ObjectBean designDataBean = importBean.addObject( "NIODesignData" );
		ObjectBean designDataItem = importBean.addObject( "Item" );
		designDataItem.addAttributeBean( "name" , getUniqueName( name )  ,  "trueAndAutoAdjust" );
		
		ObjectBean designDataVariant = importBean.addObject( "Variant" );
		designDataVariant.addAttributeBean( "item" , designDataItem.getId() ,  "false" );
		designDataVariant.addAttributeBean( "name" , "V1",  "false" );
		designDataVariant.addAttributeBean( "nioMaterial" , nioMaterial , "false" );
		designDataVariant.addAttributeBean( "nioMatSpec"  , nioMatSpec  , "false" );
		
		ObjectBean dsBean = importBean.addObject( "MatDataSource" );
		ObjectBean dsItem = importBean.addObject( "Item" );
		dsItem.addAttributeBean( "name" , getUniqueName( "DS" ) , "trueAndAutoAdjust" );
		
		ObjectBean dsVariant = importBean.addObject( "Variant" );
		dsVariant.addAttributeBean( "item" , dsItem.getId() , "false" );
		dsVariant.addAttributeBean( "name" , "V1" , "false" );
		
		
		dsBean.addAttributeBean( "name", getUniqueName( "DS" ) , "false" ); 
		dsBean.addAttributeBean( "schema", matSchemal.getId() , "false" );
		dsBean.addAttributeBean( "variant", dsVariant.getId() , "false" );
		
		
		designDataBean.addAttributeBean("name", name , "false" );
		designDataBean.addAttributeBean("matSpecNumber",matSpecNumber, "false" );
		designDataBean.addAttributeBean("materialSymbol", materialSymbol , "false" );
		designDataBean.addAttributeBean("matId", matId , "false" );
		designDataBean.addAttributeBean("description","New NIO Design Data", "false" );
		designDataBean.addAttributeBean("variant" , designDataVariant.getId() , "false" );
		//designDataBean.addAttributeBean("generatedFromModel", new ImportBean[]{ nioMaterial , nioMatSpec , dsBean }   , "false" );//TODO
		designDataBean.addAttributeBean("generatedFromModel",  dsBean   , "false" );
		
		//designDataBean.addAttributeBean("generatedFromObject","", "false" );//TODO
		//designDataBean.addAttributeBean("matRevisableProperties","", "false" );//TODO
		
		String message = importBean.getXMLString();
		File xmlFile   = importBean.getXMLFile( scriptWorkingDir , "structure.xml" );
System.out.println( message );


		String zipFilePath = scriptWorkingDir + "MaterialEditor.zip" ;
		FileOutputStream fos=new java.io.FileOutputStream(zipFilePath); 
		ArchiveOutputStream zaos = new org.apache.commons.compress.archivers.ArchiveStreamFactory().createArchiveOutputStream("zip",new java.io.FileOutputStream(zipFilePath) );
		 
		ZipArchiveEntry zipArchiveEntry = new org.apache.commons.compress.archivers.zip.ZipArchiveEntry( xmlFile.getName() );    
		//添加成员到压缩包对象中    
		zaos.putArchiveEntry(zipArchiveEntry);     
		//获取当前文件的输入流    
		FileInputStream fis = new java.io.FileInputStream( xmlFile );
		//将输入流内容读取到压缩包输出流中    
		org.apache.commons.compress.utils.IOUtils.copy( fis , zaos);     
		//清除entry对象准备添加下一个文件   
		zaos.closeArchiveEntry();    
		fis.close(); 
		zaos.finish();
		fos.close();
		return zipFilePath ;
	}
	
	public  String getUniqueName( String prefix ) {
		//return prefix + CoreWidgetsUtils.generateUnqiueWidgetName();
		return prefix + Long.toHexString( timeSeq++ ) ;
	}
	
}
